package com.itheima.controller.ums;

import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.ums.UmsMenu;
import com.itheima.pojo.ums.UmsResource;
import com.itheima.pojo.ums.UmsRole;
import com.itheima.pojo.ums.vo.UmsResourceVo;
import com.itheima.pojo.ums.vo.UmsRoleVo;
import com.itheima.service.ums.UmsRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
public class UmsRoleController {
    @DubboReference
    private UmsRoleService umsRoleService;

    /**
     * 添加角色
     *
     * @param umsRole
     * @return
     */
    @PostMapping("/create")
    private R create(@RequestBody UmsRole umsRole) {
        umsRoleService.create(umsRole);
        return R.success("success");
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    private R delete(@RequestParam List<Long> ids) {
        umsRoleService.delete(ids);
        return R.success();
    }

    /**
     * 根据角色名称分页获取角色列表
     *
     * @param keyword
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    private R list(String keyword, Integer pageSize, Integer pageNum) {
        PageInfo umsRoleList = umsRoleService.roleList(keyword, pageSize, pageNum);
        return R.success(umsRoleList);
    }

    /**
     * 获取所有角色
     *
     * @return
     */
    @GetMapping("/listAll")
    private R listAll() {
        List<UmsRole> umsRoleList = umsRoleService.listAll();
        return R.success(umsRoleList);
    }

    /**
     * 获取角色相关菜单
     * @param roleId
     * @return
     */
    @GetMapping("/listMenu/{roleId}")
    private R listMenu(@PathVariable Integer roleId) {
        List<UmsMenu> umsMenuList = umsRoleService.listMenu(roleId);
        return R.success(umsMenuList);
    }

    /**
     * 获取角色相关资源
     * @param roleId
     * @return
     */
    @GetMapping("/listResource/{roleId}")
    private R listResource(@PathVariable  Integer roleId){
        List<UmsResource> umsResourceList=umsRoleService.listResource(roleId);
        return R.success(umsResourceList);
    }

    /**
     * 修改角色
     * @param umsRole
     * @return
     */
    @PostMapping("/update/{id}")
    private R update(@RequestBody UmsRole umsRole){
        umsRoleService.updateUmsRole(umsRole);
        return R.success("success");
    }

    /**
     * 修改角色状态
     * @param id
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    private R updateStatus(@PathVariable("id") Integer id,Long status){
        umsRoleService.updateStatus(id,status);
        return R.success("success");
    }

    /**
     * 给角色分配菜单
     * @param umsRoleVo
     * @return
     */
    @PostMapping("/allocMenu")
    private R allocMenu(UmsRoleVo umsRoleVo){
        umsRoleService.allocMenu(umsRoleVo);
        return R.success("success");
    }

    /**
     * 给角色分配资源
     * @param umsResourceVo
     * @return
     */
    @PostMapping("/allocResource")
    private R allocResource(UmsResourceVo umsResourceVo){
        umsRoleService.allocResource(umsResourceVo);
        return R.success("success");
    }
}
